package com.flycms.web.front.rest;

import com.flycms.core.base.BaseController;
import com.flycms.core.entity.RestDataVo;
import com.flycms.module.favorite.service.FavoriteRestService;
import com.flycms.module.favorite.service.FavoriteService;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Open source house, All rights reserved
 * 版权：宏石<br/>
 * 开发公司：宏石<br/>
 *
 * @author sun-kaifei
 * @version 1.0 <br/>
 * @email xxx@qq.com
 * @Date: 14:25 2020/9/6
 */
@Controller
@RequestMapping(value = "/rest")
public class FavoriteRestController extends BaseController {

    @Autowired
    protected FavoriteRestService favoriteService;


    //处理关注信息
    @ResponseBody
    @PostMapping(value = "/u/favorite/add")
    public RestDataVo addFavorite(@RequestParam(value = "id", required = false) String id, @RequestParam(value = "type", required = false) String type) {
        RestDataVo data;
        try {
            if (!NumberUtils.isNumber(id)) {
                return RestDataVo.ERROR("话题id参数错误");
            }
            if (!NumberUtils.isNumber(type)) {
                return RestDataVo.ERROR("话题id参数错误");
            }
            if(getUser()==null){
                return RestDataVo.ERROR("请登陆后关注");
            }
            data=favoriteService.addFavorite(getUser().getUserId(),Integer.valueOf(type),Long.parseLong(id));
        } catch (Exception e) {
            data = RestDataVo.ERROR(e.getMessage());
        }
        return data;
    }

}
